package com.itheima.Service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.Mapper.ProductMapper;
import com.itheima.Service.ProductService;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Product;
import com.itheima.pojo.ProductQueryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult<Product> page(ProductQueryParam productQueryParam) {
        // 设置分页参数
        PageHelper.startPage(productQueryParam.getPage(), productQueryParam.getPageSize());
        // 执行查询
        List<Product> productList = productMapper.list(productQueryParam);
        // 解析分页结果
        Page<Product> page = (Page<Product>) productList;
        return new PageResult<Product>((int) page.getTotal(), productList);
    }

    @Override
    public Product get(Integer id) {
        return productMapper.get(id);
    }

    @Override
    public void add(Product product) {
        // 补全基础信息
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        product.setStatus(String.valueOf(product.getStatus().toString().equals("active")?1:0)); // 默认上架状态
        productMapper.insert(product);
    }

    @Override
    public void update(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        productMapper.update(product);
    }

    @Override
    public void updateStatus(Integer id, String status) {
        productMapper.updateStatus(id, status);
    }

    @Override
    public void delete(Integer id) {
        productMapper.delete(id);
    }

    @Override
    public List<Product> select4() {

        return productMapper.select4();
    }

    @Override
    public List<Product> selectNew() {
        return productMapper.selectNew();
    }
}